package com.mzy.olddate.demo.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author mzy
 * @Date 2021/8/17 6:57
 * @Version 1.0
 */
public class MapPerformance {
    public static void main(String[] args) {
        System.out.println("---------------------------");
//    new HashMap<String, List<User>>();

        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();

        String str[]=new String[]{"a","b","c","d","e","a","q"};
        for (int i = 0; i < 100000; i++) {
            int random = (int) Math.random()*10;
            String key = String.valueOf(str[random] + i * 100);
            Integer value = i;
            stringIntegerHashMap.put(key, value);
        }


        forEach(stringIntegerHashMap);

        forEachTwo(stringIntegerHashMap);

        forEachThree(stringIntegerHashMap);

        forFourth(stringIntegerHashMap);

    }

    /**
     * forEach循环。迭代entries对象
     * @param hashMap
     */
    public static void forEach(HashMap<String, Integer>  hashMap){

        long l = System.currentTimeMillis();
        for (Map.Entry<String, Integer> entry:
                hashMap.entrySet()) {
//            System.out.println(entry.getKey()+"####"+entry.getValue());
        }
        long l1 = System.currentTimeMillis();
        System.out.println("迭代entries对象:  "+(l1-l));
    }

    /**
     * 使用For-Each迭代keys和values
     * @param hashMap
     */
    public static void forEachTwo(HashMap<String, Integer> hashMap){

        long l = System.currentTimeMillis();
        for (String key: hashMap.keySet()) {
//            System.out.println("Key = " + key);
        }
        long l1 = System.currentTimeMillis();
        System.out.println("迭代keys和values:  "+(l1-l));

//        for (Integer value: hashMap.values()) {
//            System.out.println("Value = " + value);
//        }
    }

    /**
     * 使用Iterator迭代
     * @param hashMap
     */
    public static void forEachThree(HashMap<String, Integer> hashMap){
        Iterator<Map.Entry<String, Integer>> entryIterator = hashMap.entrySet().iterator();
        long l = System.currentTimeMillis();
        while (entryIterator.hasNext()){
            Map.Entry<String, Integer> entry = entryIterator.next();
            String key = entry.getKey();
            Integer value = entry.getValue();
//            System.out.println("key:"+key+"  value:"+value);
        }

        long l1 = System.currentTimeMillis();
        System.out.println("使用Iterator迭代:  "+(l1-l));

    }

    /**
     * 迭代keys并根据key获得values（低效的）
     * @param hashMap
     */
    public static void forFourth(HashMap<String, Integer> hashMap){
        long l = System.currentTimeMillis();
        for (String key:hashMap.keySet()
        ) {
            Integer student = hashMap.get(key);
//            System.out.println("key:"+key+"  value:"+student);
        }
        long l1 = System.currentTimeMillis();
        System.out.println("迭代keys并根据key获得values:  "+(l1-l));

    }

}
